package com.un.ebs.product.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.un.ebs.product.domain.ProductClassAttr;
import com.un.ebs.product.domain.ProductClassAttrValue;
import com.un.ebs.product.dto.ProductClassAttrValueAddDto;
import com.un.ebs.product.dto.ProductClassAttrValueUpdateDto;
import com.un.ebs.product.mapper.ProductClassAttrValueMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.un.ebs.product.service.ProductClassAttrValueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.un.ebs.core.util.WebResult;
import com.un.ebs.core.util.WebUtils;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liubo
 * @since 2019-09-16
 */
@RestController
@RequestMapping("/productClassAttrValue")
@Api(value = "product-class-attr-value", description = "品类属性可选值")
public class ProductClassAttrValueController {

    @Autowired
    private ProductClassAttrValueService service;

    @Autowired
    private ProductClassAttrValueMapper productClassAttrValueMapper;

    @PostMapping("add")
    @ApiOperation(value = "add", notes = "添加")
    public WebResult save(@RequestBody @Validated ProductClassAttrValueAddDto req) {
        service.add(req);
        return WebUtils.success();
    }

    @PostMapping("update")
    @ApiOperation(value = "update", notes = "修改")
    public WebResult update(@RequestBody ProductClassAttrValueUpdateDto req) {
        service.update(req);
        return WebUtils.success();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "getById", notes = "根据主键获取单个 ProductClassAttrValue")
    public WebResult getById(@PathVariable("id") String id) {
        return WebUtils.success(service.queryById(id));
    }

    @GetMapping("list/{attrId}")
    @ApiOperation(value = "list", notes = "")
    public WebResult list(@PathVariable("attrId") String attrId){
        LambdaQueryWrapper<ProductClassAttrValue> lq = Wrappers.lambdaQuery();
        lq.eq(ProductClassAttrValue::getClassAttrId,attrId);
        lq.eq(ProductClassAttrValue::getIsDelete,false);
        lq.orderByAsc(ProductClassAttrValue::getCode);
        return WebUtils.success(productClassAttrValueMapper.selectList(lq));
    }

    @PostMapping("delete/{id}")
    @ApiOperation(value = "delete", notes = "根据主键删除单个 ProductClassAttrValue")
    public WebResult delete(@PathVariable("id") String id) {
        service.delete(id);
        return WebUtils.success();
    }

    @PostMapping("changeSort")
    @ApiOperation(value = "changeSort", notes = "修改 ProductClassAttrValue 的序号")
    public WebResult changeSort(@RequestParam String id, @RequestParam int index) {
        service.changeSort(id, index);
        return WebUtils.success();
    }

}